﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

using JSales.ModuleSystem;
using JSales.ModuleSales;
using JSales.ModuleProduct;

namespace JSales
{
    /// <summary>
    /// MDI主窗体
    /// </summary>
    public partial class MainMDI : Form
    {
        private int childFormNumber = 0;

        /// <summary>
        /// 初始化
        /// </summary>
        public MainMDI()
        {
            InitializeComponent();
            ApplyRight();
        }

        public void ApplyRight()
        {
            if (!CheckRight.CheckModel(int.Parse(GlobalVar.cUser.UserID.ToString()), 0)) 
                SystemMenu.Visible=false;
            if (!CheckRight.CheckModel(int.Parse(GlobalVar.cUser.UserID.ToString()), 1))
                ToolStripMenuItemCustomer.Visible = false;
            if (!CheckRight.CheckModel(int.Parse(GlobalVar.cUser.UserID.ToString()), 2))
                ToolStripMenuItemProduct.Visible = false;
            if (!CheckRight.CheckModel(int.Parse(GlobalVar.cUser.UserID.ToString()), 3))
                ToolStripMenuItemSales.Visible = false;

        }

        /// <summary>
        /// 显示窗体
        /// </summary>
        /// <param name="sender">响应</param>
        /// <param name="e">事件</param>
        private void ShowNewForm(object sender, EventArgs e)
        {
            dbsetForm fm = null;
            foreach (Form ftmp in this.MdiChildren)
            {
                if (ftmp is dbsetForm)
                {
                    fm = (dbsetForm)(ftmp);
                    break;
                }
            }
            if (fm == null || fm.Disposing)
            {
                fm = new dbsetForm();
                fm.MdiParent = this;
                fm.WindowState = FormWindowState.Normal;
            }
            fm.Show();
            fm.Activate();
        }

        private void DeptToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //OpenFileDialog openFileDialog = new OpenFileDialog();
            //openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            //openFileDialog.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            //if (openFileDialog.ShowDialog(this) == DialogResult.OK)
            //{
            //    string FileName = openFileDialog.FileName;
            //}
            DeptForm fm = null;
            foreach (Form ftmp in this.MdiChildren)
            {
                if (ftmp is DeptForm)
                {
                    fm = (DeptForm)(ftmp);
                    break;
                }
            }
            if (fm == null || fm.Disposing)
            {
                fm = new DeptForm();
                fm.MdiParent = this;
                fm.WindowState = FormWindowState.Normal;
            }
            fm.Show();
            fm.Activate();

        }

        private void RightsToolStripMenuItemClick(object sender, EventArgs e)
        {
            //SaveFileDialog saveFileDialog = new SaveFileDialog();
            //saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            //saveFileDialog.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            //if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
            //{
            //    string FileName = saveFileDialog.FileName;
            //}
            FrmRights fm = null;
            foreach (Form ftmp in this.MdiChildren)
            {
                if (ftmp is FrmRights)
                {
                    fm = (FrmRights)(ftmp);
                    break;
                }
            }
            if (fm == null || fm.Disposing)
            {
                fm = new FrmRights();
                fm.MdiParent = this;
                fm.WindowState = FormWindowState.Normal;
            }
            fm.Show();
            fm.Activate();
        }

        private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void TileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
        }

        private void MainMDI_Load(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void Exit_ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox fm = null;
            foreach (Form ftmp in this.MdiChildren)
            {
                if (ftmp is AboutBox)
                {
                    fm = (AboutBox)(ftmp);
                    break;
                }
            }
            if (fm == null || fm.Disposing)
            {
                fm = new AboutBox();
                fm.MdiParent = this;
                fm.WindowState = FormWindowState.Normal;
            }
            fm.Show();
            fm.Activate();
        }

        private void 订单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OrderForm fm = null;
            foreach (Form ftmp in this.MdiChildren)
            {
                if (ftmp is OrderForm)
                {
                    fm = (OrderForm)(ftmp);
                    break;
                }
            }
            if (fm == null || fm.Disposing)
            {
                fm = new OrderForm();
                fm.MdiParent = this;
                fm.WindowState = FormWindowState.Normal;
            }
            fm.Show();
            fm.Activate();
        }

        private void MainMDI_Resize(object sender, EventArgs e)
        {
            if(this.WindowState==FormWindowState.Minimized)
            {
                this.Hide();
            }
        }

        private void ToolStripMenuItemHide_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void Show_XToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Show();
            if (this.WindowState == FormWindowState.Minimized)
                this.WindowState = FormWindowState.Normal;
            this.Activate();
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            this.Show();
            if (this.WindowState == FormWindowState.Minimized)
                this.WindowState = FormWindowState.Normal;
            else if (this.WindowState == FormWindowState.Normal)
                this.WindowState = FormWindowState.Minimized;
            this.Activate();
        }
 
        private void ToolStripMenuItemRaw_Click(object sender, EventArgs e)
        {
            ProductFormRaw fm = null;
            foreach (Form ftmp in this.MdiChildren)
            {
                if (ftmp is ProductFormRaw)
                {
                    fm = (ProductFormRaw)(ftmp);
                    break;
                }
            }
            if (fm == null || fm.Disposing)
            {
                fm = new ProductFormRaw();
                fm.MdiParent = this;
                fm.WindowState = FormWindowState.Normal;
            }
            fm.Show();
            fm.Activate();
        }

        private void ToolStripMenuItemSemi_Click(object sender, EventArgs e)
        {
            ProductFormSemi fm = null;
            foreach (Form ftmp in this.MdiChildren)
            {
                if (ftmp is ProductFormSemi)
                {
                    fm = (ProductFormSemi)(ftmp);
                    break;
                }
            }
            if (fm == null || fm.Disposing)
            {
                fm = new ProductFormSemi();
                fm.MdiParent = this;
                fm.WindowState = FormWindowState.Normal;
            }
            fm.Show();
            fm.Activate();
        }

        private void ToolStripMenuItemEnd_Click(object sender, EventArgs e)
        {
            ProductForm fm = null;
            foreach (Form ftmp in this.MdiChildren)
            {
                if (ftmp is ProductForm)
                {
                    fm = (ProductForm)(ftmp);
                    break;
                }
            }
            if (fm == null || fm.Disposing)
            {
                fm = new ProductForm();
                fm.MdiParent = this;
                fm.WindowState = FormWindowState.Normal;
            }
            fm.Show();
            fm.Activate();
        }

        #region 修改密码
        /// <summary>
        /// 修改密码事件
        /// </summary>
        /// <param name="sender">响应</param>
        /// <param name="e">事件</param>
        private void 修改密码ToolStripMenuItemClick(object sender, EventArgs e)
        {
            ModifyPswdForm fm = null;
            foreach (Form ftmp in this.MdiChildren)
            {
                if (ftmp is ModifyPswdForm)
                {
                    fm = (ModifyPswdForm)(ftmp);
                    break;
                }
            }
            if (fm == null || fm.Disposing)
            {
                fm = new ModifyPswdForm();
                fm.MdiParent = this;
                fm.WindowState = FormWindowState.Normal;
            }
            fm.Show();
            fm.Activate();
        }
        #endregion
        private void UserToolStripMenuItemClick(object sender, EventArgs e)
        {
            UserManageForm fm = null;
            foreach (Form ftmp in this.MdiChildren)
            {
                if (ftmp is UserManageForm)
                {
                    fm = (UserManageForm)(ftmp);
                    break;
                }
            }
            if (fm == null || fm.Disposing)
            {
                fm = new UserManageForm();
                fm.MdiParent = this;
                fm.WindowState = FormWindowState.Normal;
            }
            fm.Show();
            fm.Activate();
        }

        private void 颜色定义ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ColorSetForm fm = null;
            foreach (Form ftmp in this.MdiChildren)
            {
                if (ftmp is ColorSetForm)
                {
                    fm = (ColorSetForm)(ftmp);
                    break;
                }
            }
            if (fm == null || fm.Disposing)
            {
                fm = new ColorSetForm();
                fm.MdiParent = this;
                fm.WindowState = FormWindowState.Normal;
            }
            fm.Show();
            fm.Activate();
        }

        private void KindStripMenuItem_Click(object sender, EventArgs e)
        {
            KindSetForm fm = null;
            foreach (Form ftmp in this.MdiChildren)
            {
                if (ftmp is KindSetForm)
                {
                    fm = (KindSetForm)(ftmp);
                    break;
                }
            }
            if (fm == null || fm.Disposing)
            {
                fm = new KindSetForm();
                fm.MdiParent = this;
                fm.WindowState = FormWindowState.Normal;
            }
            fm.Show();
            fm.Activate();
        }

        private void GrammageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GrammageSetForm fm = null;
            foreach (Form ftmp in this.MdiChildren)
            {
                if (ftmp is GrammageSetForm)
                {
                    fm = (GrammageSetForm)(ftmp);
                    break;
                }
            }
            if (fm == null || fm.Disposing)
            {
                fm = new GrammageSetForm();
                fm.MdiParent = this;
                fm.WindowState = FormWindowState.Normal;
            }
            fm.Show();
            fm.Activate();
        }

        private void TextureToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TextureSetForm fm = null;
            foreach (Form ftmp in this.MdiChildren)
            {
                if (ftmp is TextureSetForm)
                {
                    fm = (TextureSetForm)(ftmp);
                    break;
                }
            }
            if (fm == null || fm.Disposing)
            {
                fm = new TextureSetForm();
                fm.MdiParent = this;
                fm.WindowState = FormWindowState.Normal;
            }
            fm.Show();
            fm.Activate();
        }

        private void ProcesstoolStripMenuItem_Click(object sender, EventArgs e)
        {
            ProcessSetForm fm = null;
            foreach (Form ftmp in this.MdiChildren)
            {
                if (ftmp is ProcessSetForm)
                {
                    fm = (ProcessSetForm)(ftmp);
                    break;
                }
            }
            if (fm == null || fm.Disposing)
            {
                fm = new ProcessSetForm();
                fm.MdiParent = this;
                fm.WindowState = FormWindowState.Normal;
            }
            fm.Show();
            fm.Activate();
        }

        private void PriceToolStripMenuItemClick(object sender, EventArgs e)
        {
            PriceSetForm fm = null;
            foreach (Form ftmp in this.MdiChildren)
            {
                if (ftmp is PriceSetForm)
                {
                    fm = (PriceSetForm)(ftmp);
                    break;
                }
            }
            if (fm == null || fm.Disposing)
            {
                fm = new PriceSetForm();
                fm.MdiParent = this;
                fm.WindowState = FormWindowState.Normal;
            }
            fm.Show();
            fm.Activate();
        }

        private void 客户ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CustomerForm fm = null;
            foreach (Form ftmp in this.MdiChildren)
            {
                if (ftmp is CustomerForm)
                {
                    fm = (CustomerForm)(ftmp);
                    break;
                }
            }
            if (fm == null || fm.Disposing)
            {
                fm = new CustomerForm();
                fm.MdiParent = this;
                fm.WindowState = FormWindowState.Normal;
            }
            fm.Show();
            fm.Activate();
        }

        #region 刷新基础数据
        private void ToolStripMenuItemFresh_Click(object sender, EventArgs e)
        {
            GlobalVar.SelectDept();
            toolStripStatusLabel.Text = "加载部门信息";
            this.Refresh();

            GlobalVar.SelectRole();
            toolStripStatusLabel.Text = "加载角色信息";
            this.Refresh();

            GlobalVar.SelectUsers();
            toolStripStatusLabel.Text = "加载用户信息";
            this.Refresh();

            GlobalVar.SelectRight();
            toolStripStatusLabel.Text = "加载权限信息";
            this.Refresh();

            GlobalVar.SelectCustomer();
            toolStripStatusLabel.Text = "加载客户信息";
            this.Refresh();

            GlobalVar.SelectProduct();
            toolStripStatusLabel.Text = "加载产品信息";
            this.Refresh();

            GlobalVar.SelectKind();
            toolStripStatusLabel.Text = "加载类型信息";
            this.Refresh();

            GlobalVar.SelectPrice();
            toolStripStatusLabel.Text = "加载价格信息";
            this.Refresh();

            GlobalVar.SelectDegree();
            toolStripStatusLabel.Text = "加载程度信息";
            this.Refresh();

            GlobalVar.SelectColor();
            toolStripStatusLabel.Text = "加载颜色信息";
            this.Refresh();

            GlobalVar.SelectGrammage();
            toolStripStatusLabel.Text = "加载克重信息";
            this.Refresh();

            GlobalVar.SelectTexture();
            toolStripStatusLabel.Text = "加载纹路信息";
            this.Refresh();

            GlobalVar.SelectProcess();
            toolStripStatusLabel.Text = "加载工艺信息";
            this.Refresh();

            GlobalVar.SelectLevel();
            toolStripStatusLabel.Text = "加载等级信息";
            this.Refresh();

            GlobalVar.SelectStore();
            toolStripStatusLabel.Text = "加载仓库信息";
            this.Refresh();

            toolStripStatusLabel.Text = "刷新完成";
            this.Refresh();
        }
        #endregion 



        private void toolStripMenuItemLevel_Click(object sender, EventArgs e)
        {
            XtraFormLevel fm = null;
            foreach (Form ftmp in this.MdiChildren)
            {
                if (ftmp is XtraFormLevel)
                {
                    fm = (XtraFormLevel)(ftmp);
                    break;
                }
            }
            if (fm == null || fm.Disposing)
            {
                fm = new XtraFormLevel();
                fm.MdiParent = this;
                fm.WindowState = FormWindowState.Normal;
            }
            fm.Show();
            fm.Activate();
        }

        private void ToolStripMenuItemRoles_Click(object sender, EventArgs e)
        {
            FormRoles fm = null;
            foreach (Form ftmp in this.MdiChildren)
            {
                if (ftmp is FormRoles)
                {
                    fm = (FormRoles)(ftmp);
                    break;
                }
            }
            if (fm == null || fm.Disposing)
            {
                fm = new FormRoles();
                fm.MdiParent = this;
                fm.WindowState = FormWindowState.Normal;
            }
            fm.Show();
            fm.Activate();
        }




    }
}
